<?php
class content_commonModel extends baseModel{
	protected $table = 'content';

    //访问计数
    public function views_content($aid,$views){
        $data['views'] = $views + 1;
        $condition['aid'] = $aid;
        $this->update($condition,$data);
    }

    //上一篇
    public function prev_content($aid,$category){
        //内容信息SQL接口
        $ext_sql='';
        $ext_field='';
        $ext_where='';
        $api_sql=ApiHook('hook_content_prev_sql',$category);
        if(!empty($api_sql)){
            foreach ($api_sql as $value) {
                $ext_sql.=$value['sql'];
                $ext_field.=$value['field'];
                $ext_where.=$value['where'];
            }
        }
        $info="
            SELECT {$$ext_field}A.*
             FROM {$this->model->pre}content A 
             LEFT JOIN {$this->model->pre}category B ON B.cid = A.cid
             {$$ext_sql}
             WHERE A.aid<{$aid} AND A.status=1 AND B.cid={$category['cid']}{$ext_where} ORDER BY A.aid desc LIMIT 1
            ";
            $info=$this->model->query($info);
        return $info[0]; 
    }

    //下一篇
    public function next_content($aid,$category){
        //内容信息SQL接口
        $ext_sql='';
        $ext_field='';
        $ext_where='';
        $api_sql=ApiHook('hook_content_next_sql',$category);
        if(!empty($api_sql)){
            foreach ($api_sql as $value) {
                $ext_sql.=$value['sql'];
                $ext_field.=$value['field'];
                $ext_where.=$value['where'];
            }
        }
        $info="
            SELECT {$expand_field}A.*
             FROM {$this->model->pre}content A 
             LEFT JOIN {$this->model->pre}category B ON B.cid = A.cid
             {$expand}
             WHERE A.aid>{$aid} AND A.status=1 AND B.cid={$category['cid']}{$ext_where} ORDER BY A.aid asc LIMIT 1
            ";
            $info=$this->model->query($info);
        return $info[0];
    }

    //替换后内容
    public function replace_content($content){
        $replace = $this->model->table('replace')->select();
        $content=html_out($content);
        if (!empty($replace)) {
            foreach ($replace as $export) {
                if(empty($export['key'])){
                    if(empty($export['num'])){
                        $export['num']=1;
                    }
                    $content = preg_replace("/(?!<[^>]+)".preg_quote($export['key'],'/')."(?![^<]*>)/",$export['content'], $content,$export['num']);
                }
            }
        }
        return $content;
    }

    //增加TAG链接
    public function tag_link($content,$aid){
        $info="
            SELECT A.*
             FROM {$this->model->pre}tags A 
             LEFT JOIN {$this->model->pre}tags_relation B ON B.tid=A.id
             WHERE B.aid={$aid}
        ";
        $taglist = $this->model->query($info);
        $content=html_out($content);
        if (!empty($taglist)) {
            foreach ($taglist as $export) {
                if(!empty($export['name'])){
                    $content = preg_replace("/(?!<[^>]+)".preg_quote($export['name'],'/')."(?![^<]*>)/", '<a href="'.url('tags/list/index',array('tag'=>$export['name'])).'"  target="_blank">'.$export['name'].'</a>',$content,1);
                }
            }
        }
        return $content;
    }

}